//
//  NetworkTools.swift
//  SwiftSina
//
//  Created by 刘战军 on 16/5/16.
//  Copyright © 2016年 LiuZhanJun. All rights reserved.
//

import UIKit
import AFNetworking
/**
网络请求枚举类型, Swift中的枚举可以指定不同的类型

- GET:  GET请求
- POST: POST请求
*/
enum ZJRequestMethod: String {
    
    case GET = "GET"
    case POST = "POST"
    
}

class NetworkTools: AFHTTPSessionManager {
    
    // 会带哦方法的参数, 可以从 AFN 的方法直接粘贴
    // 成功回调
    func requset(method: ZJRequestMethod, URLSting: String, parpmeters: [String: AnyObject]?, finished:(result: [String: AnyObject]?)->()) {
        
        let success = { (task: NSURLSessionDataTask, responseObject: AnyObject?) -> Void in
            
            if let result = responseObject as? [String: AnyObject] {
                print("数据请求成功, 格式正确")
                finished(result: result)
            }else {
                
                print("网络格式错误")
                finished(result: nil)
            }
        }
        
        let failure = { (task: NSURLSessionDataTask?, error: NSError) -> Void in
            
            print("网络请求失败")
            finished(result: nil)
        }

        if method == .GET {
            // GET 请求
            GET(URLSting, parameters: parpmeters, progress: nil, success: success, failure: failure)
        }else {
            // POST 请求
            POST(URLSting, parameters: parpmeters, progress: nil, success: success, failure: failure)
        }
    
    }
}
